package ntd.util.frame.task.handle.fm.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ToolOpt;
import ntd.util.frame.msg.bo.MsgInBO;
import ntd.util.frame.msg.bo.MsgInfo;
import ntd.util.config.common.enums.msg.EnumMsgHandle;
import org.apache.commons.lang3.StringUtils;

public class UtilFmMsg {
    /**
     * 消息推送
     * @param siteName
     * @param warningType
     * @param cause
     * @param phone
     * @param alias
     */
    public static void msgPush(String siteName, String warningType, String cause, String phone, String alias) {
        // APP推送
        msgPushApp(siteName, warningType, cause, alias);
        // 短信推送
        msgPushSms(siteName, warningType, cause, phone);
    }

    public static String msgPushSms(String siteName, String warningType, String cause, String phone) {
        if (StringUtils.isBlank(phone)) {
            return siteName;
        }
        MsgInBO msgInBO = new MsgInBO();
        msgInBO.setChannelBusinessId("2c906edb7fd39df1017fd3cc9a54000b");
        msgInBO.setEnumMsgHandle(EnumMsgHandle.JPUSH_SMS);
        msgInBO.setReceiverPhones(phone);
        msgInBO.setMsgSenderUserId("-1");

        JSONObject templateParams = new JSONObject();
        templateParams.putOnce("site_name", siteName);
        templateParams.putOnce("warning_type", warningType);
        templateParams.putOnce("cause", cause);

        JSONObject jsonParams = JSONUtil.createObj();
        jsonParams.putOnce("templateCode", "207149");
//        jsonParams.putOnce("signId", "21295");
        jsonParams.putOnce("signId", "24858");
        jsonParams.putOnce("templateParams", templateParams);

        msgInBO.setDataJson(jsonParams.toString());
        return ToolOpt.OPT_MSG.putMsg(msgInBO);
    }

    public static String msgPushApp(String siteName, String warningType, String cause, String alias) {
        if (StringUtils.isBlank(alias)) {
            return siteName;
        }
        MsgInBO msgInBO = new MsgInBO();
        msgInBO.setChannelBusinessId("2c906edb7fd39df1017fd3cc9a54000b");
        msgInBO.setEnumMsgHandle(EnumMsgHandle.JPUSH_PUSH);
        msgInBO.setMsgInfo(MsgInfo.init("尊敬的用户，您管理的（"+siteName+"）出现事件（{{"+warningType+"}}），原因：{{"+cause+"}}，请尽快处理。"));
        msgInBO.setMsgSenderUserId("-1");
        msgInBO.setReceiverAlias(alias);
        return ToolOpt.OPT_MSG.putMsg(msgInBO);
    }

    /**
     * 富铭验证码
     * @param phone
     * @param code
     */
    public static void captcha(String phone, String code) {
        if (StringUtils.isBlank(phone)) {
            return;
        }
        MsgInBO msgInBO = new MsgInBO();
        msgInBO.setChannelBusinessId("2c906edb7fd39df1017fd3cc9a54000b");
        msgInBO.setEnumMsgHandle(EnumMsgHandle.JPUSH_SMS);
        msgInBO.setReceiverPhones(phone);
        msgInBO.setMsgSenderUserId("-1");

        JSONObject templateParams = new JSONObject();
        templateParams.putOnce("code", code);

        JSONObject jsonParams = JSONUtil.createObj();
        jsonParams.putOnce("templateCode", "1");
        jsonParams.putOnce("signId", "24858");
        jsonParams.putOnce("templateParams", templateParams);

        msgInBO.setDataJson(jsonParams.toString());
        ToolOpt.OPT_MSG.putMsg(msgInBO);
    }

    public static void main(String[] args) {
        captcha("18837132130", "456789");
    }
}
